package com.stylefeng.guns.modular.system.controller;

import com.stylefeng.guns.common.annotion.log.BussinessLog;
import com.stylefeng.guns.common.constant.Dict;
import com.stylefeng.guns.common.controller.BaseController;
import com.stylefeng.guns.common.persistence.dao.OpDialogueMapper;
import com.stylefeng.guns.common.persistence.dao.OrgUserMapper;
import com.stylefeng.guns.common.persistence.model.OpDialogue;
import com.stylefeng.guns.common.persistence.model.OrgUser;
import com.stylefeng.guns.core.shiro.ShiroKit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Date;
import java.util.List;

/**
 * 用户对话表控制器
 *
 * @author fengshuonan
 * @Date 2019-01-04 14:34:54
 */
@Controller
@RequestMapping("/opDialogue")
public class OpDialogueController extends BaseController {

    private String PREFIX = "/system/opDialogue/";

    @Autowired
    OpDialogueMapper opDialogueMapper;
    @Autowired
    OrgUserMapper orgUserMapper;
    @Autowired
    CheckAccessUtil checkAccessUtil;

    /**
     * 跳转到用户对话表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "opDialogue.html";
    }

    /**
     * 跳转到添加用户对话表
     */
    @RequestMapping("/opDialogue_add/{userId}")
    public String opDialogueAdd(@PathVariable Integer userId,Model model) {

        model.addAttribute("userId",userId);
        return PREFIX + "opDialogue_add.html";
    }

    /**
     * 跳转到修改用户对话表
     */
    @RequestMapping("/opDialogue_update/{opDialogueId}")
    public String opDialogueUpdate(@PathVariable Integer opDialogueId, Model model) {
        return PREFIX + "opDialogue_edit.html";
    }

    /**
     * 获取用户对话表列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<OpDialogue> opDialogues = opDialogueMapper.selectList(null);
        for(OpDialogue opDialogue:opDialogues){
            OrgUser orgUser = orgUserMapper.selectById(opDialogue.getUserId());
            Integer b =opDialogue.getUserId();
            if(b == null){
                opDialogue.setUserName(null);
            }else{
                opDialogue.setUserName(orgUser.getUserName());
            }
            Integer a = opDialogue.getType();
            if(a == 1){
                opDialogue.setTypeName("用户");
            }else if(a == 2){
                opDialogue.setTypeName("系统");
            }else {
                opDialogue.setTypeName("兔子走了");
            }
        }
        return opDialogues;
    }

    /**
     * 新增用户对话表
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(OpDialogue opDialogue,Integer userId) {
        int adminUserId = ShiroKit.getUser().getId();
        opDialogue.setUserId(userId);
        opDialogue.setAdminUserId(adminUserId);
        opDialogue.setType(2);
        opDialogue.setCreateTime(new Date());
        opDialogueMapper.insert(opDialogue);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除用户对话表
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete() {
        return SUCCESS_TIP;
    }


    /**
     * 修改用户对话表
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @BussinessLog(value = "修改节点",key = "opDialogueId",dict = Dict.DictMap)
    public Object update() {
        return super.SUCCESS_TIP;
    }

    /**
     * 用户对话表详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail() {
        return null;
    }
}
